package com.fancy.learncenter.ui.view;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonClassItemBean;
import com.fancy.learncenter.ui.adapter.CartoonClassListAdapter;
import com.fancy.learncenter.ui.callback.AlbumImpl;
import com.fancy.learncenter.ui.callback.CartoonAddClassImpl;

import java.util.List;

/**
 * Created by shiweiwei on 2018/1/5.
 */

public class CartoonListClassPopu extends BasePopupWindow {

    private View rootView;
    private Activity mContext;
    private CustomRecycleView recyclerView;
    private CartoonClassListAdapter adapter;
    private List<CartoonClassItemBean> list;
    private View layoutView;
    private ImageView addClassImage;

    public void setListener(CartoonAddClassImpl listener) {
        this.listener = listener;
    }

    private CartoonAddClassImpl listener;

    public CartoonListClassPopu(Activity mContext, View layoutView, List<CartoonClassItemBean> list) {
        super(mContext);
        this.mContext = mContext;
        this.list = list;
        initView();
        this.layoutView = layoutView;
    }

    public void setDatas(List<CartoonClassItemBean> list) {
        this.list = list;
        adapter.notifyDataSetChanged();
    }

    private void initView() {

        rootView = LayoutInflater.from(mContext).inflate(R.layout.popu_cartoon_list_class, null);
        recyclerView = (CustomRecycleView) rootView.findViewById(R.id.recyclerView);
        addClassImage = (ImageView) rootView.findViewById(R.id.add_class_image);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(rootView);
        adapter = new CartoonClassListAdapter(mContext, list);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);

        adapter.setListener(new AlbumImpl() {
            @Override
            public void onClick(int position) {

                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });

        addClassImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.addClass();
                }

            }
        });

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
            }
        });

    }

    public void setItemDatas(List<CartoonClassItemBean> list) {
        adapter.notifyDataSetChanged(list);
    }

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();

        showAtLocation(layoutView, Gravity.CENTER, 0, 0);
    }


}
